package com.example.springboot.system.controller;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.resp.Result;
import com.example.springboot.common.utils.JwtToken;
import com.example.springboot.system.entity.ReviewEntity;
import com.example.springboot.system.mapper.ReviewMapper;
import com.example.springboot.system.service.ReviewService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author jiang
 * @since 2025-04-17
 */
@RestController
@RequestMapping("/reviewEntity")
public class ReviewController {


    private final ReviewService service;
    private final ReviewMapper mapper;

    public ReviewController(ReviewService service, ReviewMapper mapper) {
        this.service = service;
        this.mapper = mapper;
    }

    @JwtToken
    @Operation(summary = "列表")
    @GetMapping("/reviewList")
    public Result<?> reviewList() {
        return Result.success(service.list());
    }

    @JwtToken
    @Operation(summary = "获取")
    @PostMapping("/reviewId")
    public Result<?> reviewId(@RequestParam Integer id) {
        return Result.success(mapper.selectById(id));
    }

    @JwtToken
    @Operation(summary = "列表分页")
    @PostMapping("/reviewListPage")
    public Result<?> reviewListPage(@RequestParam Integer page, @RequestParam Integer pageSize) {
        Page<ReviewEntity> rowPage = new Page<>(page, pageSize);
        QueryWrapper<ReviewEntity> wrapper = new QueryWrapper<>();
        rowPage = mapper.selectPage(rowPage, wrapper);
        return Result.success(rowPage);
    }

    @Data
    static class Review {
        private Integer newsId;
        private Integer userId;
        private String content;
        private Date current;
    }

    @JwtToken
    @Operation(summary = "新增")
    @PostMapping("/reviewAppend")
    public Result<?> reviewAppend(@RequestBody ReviewController.Review review) {
        ReviewEntity entity = new ReviewEntity();
        BeanUtils.copyProperties(review, entity);
        return Result.success(service.save(entity));
    }

    @JwtToken
    @Operation(summary = "删除")
    @PostMapping("/reviewDelete")
    public Result<?> reviewDelete(@RequestParam Integer id) {
        return Result.success(service.removeById(id));
    }

    @JwtToken
    @Operation(summary = "编辑")
    @PostMapping("/reviewEditor")
    public Result<?> reviewEditor(@RequestBody ReviewEntity reviewEntity) {
        QueryWrapper<ReviewEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id", reviewEntity.getId());
        return Result.success(service.updateById(reviewEntity));
    }

    @JwtToken
    @Operation(summary = "搜索")
    @PostMapping("/reviewSearch")
    public Result<?> reviewSearch(@RequestParam String name) {
        QueryWrapper<ReviewEntity> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        return Result.success(mapper.selectList(wrapper));
    }

}
